package com.tianqi.code.lottery.domain.strategy.service.algorithm.Impl;

import com.tianqi.code.lottery.domain.strategy.service.algorithm.AbstractHashDrawAlgorithm;
import org.springframework.stereotype.Service;

@Service("fibonacciHashDrawAlgorithm")
public class FibonacciHashDrawAlgorithm extends AbstractHashDrawAlgorithm {

    // 数组初始化长度
    private final int RATE_TUPLE_LENGTH = 128;

    @Override
    protected int getRateTupleLength() {
        return RATE_TUPLE_LENGTH;
    }

    @Override
    protected int getIndex(int i) {

        // 斐波那契散列增量，逻辑：黄金分割点：(√5 - 1) / 2 = 0.6180339887，Math.pow(2, 32) * 0.6180339887 = 0x61c88647
        int HASH_INCREMENT = 0x61c88647;

        int hashCode = i * HASH_INCREMENT + HASH_INCREMENT;
        return hashCode & (RATE_TUPLE_LENGTH - 1);
    }
}
